﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library;
using FRL.DebtChased.Silverlight.Library.RichContentService;

namespace FRL.DebtChased.ChaseWorkspace
{

	/// <summary>
	/// Declaration of ClientAndDebtorPanelCtrl class
	/// Control displaying client and debtor details,
	/// client details are read-only, debtor details are overlayed
	/// </summary>
	public partial class ClientAndDebtorPanelCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Construction

		public ClientAndDebtorPanelCtrl()
		{

			// Load xaml resources
			InitializeComponent();

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		private void DebtorHeadingBarCtrl_ExpandChange(object sender, EventArgs e)
		{

			// TODO
			// Once this design is approved, convert this to a XAML animation

			// Get height of client details row
			double dblRowHeight = LayoutRoot.RowDefinitions[1].ActualHeight;

			if (dblRowHeight > LayoutRoot.RowDefinitions[1].MinHeight)
			{
				LayoutRoot.RowDefinitions[1].Height = new GridLength(
					LayoutRoot.RowDefinitions[1].MinHeight, 
					GridUnitType.Pixel
				);
				LayoutRoot.RowDefinitions[3].Height = new GridLength(1, GridUnitType.Star);
				DebtorHeadingBarCtrl.IsArrowUp = false;
			}
			else
			{
				LayoutRoot.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Star);
				LayoutRoot.RowDefinitions[3].Height = new GridLength(
					LayoutRoot.RowDefinitions[3].MinHeight,
					GridUnitType.Pixel
				);
				DebtorHeadingBarCtrl.IsArrowUp = true;
			}

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public void PostSupportDataLoad()
		{

			// Called after plug-in has loaded support data

			// Load the trading styles
			foreach (var di in CachedData.Singleton.TradingStyleMap)
				cboDebtorTradingStyle.Items.Add( new ComboBoxObject(di.Value,di.Key,null));
			cboDebtorTradingStyle.SelectedIndex = 0;

		}

		public void Set(AccountTransport account)
		{

			// Set primary client contact details
			clientDetails.Set(
				account.PrimaryClient.PrimaryContact,
				account.PrimaryClient.BankAccounts
			);

			// Set client contact details
            debtorContacts.Contacts = account.DebtorContacts;

			// Set controls
			txtDebtorCompany.Text = account.DebtorName;
			ComboBoxObject.SelectByItemData(cboDebtorTradingStyle, account.TradingStyle);

		}

		public List<ContactPointTransport> GetDebtorContactPoints(bool bIncludeEmail)
		{

			// Call debtor control
			return debtorContacts.GetPointList(bIncludeEmail ,- 1);

		}

	}

}
